package com.eeepay.eeepay_v2.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.activity.AgentQueryActivity;
import com.eeepay.eeepay_v2.activity.DeviceListActivity;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.v2_library.util.ScreenSwitch;
import com.eeepay.v2_library.view.HorizontalItemView;
import com.eeepay.v2_library.view.LeftRightText;
import com.eeepay.v2_library.view.TitleBar;

/**
 * Created by zw on 2016/3/25 0025.
 * 机具下发  、 机具回收  popWindow
 */
public class DeviceOperationPopView extends PopupWindow implements View.OnClickListener {

    private boolean isAll;
    private int mode; //模式    0机具下发、   1机具回收
    private Context mContext;
    private View view;// 视图
    private TextView tv;
    private LeftRightText lrt_deviceCount;
    private HorizontalItemView hv_agentName;
    private Button btn_confirm;
    private View layout;

    private int count = 1;//机具批量下发时的数量

    private OnCompleteListener listener;

    public void setOnCompleteListener(OnCompleteListener listener) {
        this.listener = listener;
    }

    public DeviceOperationPopView(Context context, int mode) {
        super(context);
        this.mContext = context;
        this.mode = mode;
        init();
        setWidget();

    }


    private void init() {
        view = View.inflate(mContext, R.layout.pop_device_issue_recycle, null);
        //设置PopupWindow的View
        this.setContentView(view);
        this.setFocusable(true);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //菜单背景色
        ColorDrawable dw = new ColorDrawable(Constant.GRAY_LUCENCY_COLOR);
        this.setBackgroundDrawable(dw);

        tv = (TextView) view.findViewById(R.id.tv_recycle_device);
        lrt_deviceCount = (LeftRightText) view.findViewById(R.id.lrt_device_count);
        hv_agentName = (HorizontalItemView) view.findViewById(R.id.hv_agent_name);
        btn_confirm = (Button) view.findViewById(R.id.btn_confirm);
        layout = view.findViewById(R.id.layout);


    }

    private void setWidget() {
        layout.setOnClickListener(this);
        hv_agentName.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);
    }

    /**
     * 设置popwindow的展示类型
     * @param mode
     */
    public void setMode(int mode) {
        this.mode = mode;
        switch (mode){
            case Constant.DEVICE_ISSUE:
                tv.setVisibility(View.GONE);
                lrt_deviceCount.setVisibility(View.VISIBLE);
                hv_agentName.setVisibility(View.VISIBLE);
                break;
            case Constant.DEVICE_RECYCLE:
                tv.setVisibility(View.VISIBLE);
                lrt_deviceCount.setVisibility(View.GONE);
                hv_agentName.setVisibility(View.GONE);
                break;
        }
    }

    public void setIsAll(boolean isAll){
        this.isAll = isAll;
    }

    public void setCount(int count){
        this.count = count;
        lrt_deviceCount.setRightText(count + "个");
    }

    public void setDatas(String agentName){
        hv_agentName.setRightText(agentName);
    }

    @Override
    public void dismiss() {
        super.dismiss();
       if(isAll){
            ((DeviceListActivity) mContext).getTitleBar().setShowRight(View.GONE);
       }else {
           ((DeviceListActivity) mContext).getTitleBar().setRightTextView("确定");
       }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout:
                dismiss();
                break;
            case R.id.hv_agent_name:
                Bundle bundle = new Bundle();
                bundle.putString(Constant.INTENT_FLAG, Constant.DEVICE_ISSUE_FLAG);
                ScreenSwitch.switchActivity(mContext, AgentQueryActivity.class, null, Constant.RequestCode.agent_all);
                break;
            case R.id.btn_confirm:
                if(mode == Constant.DEVICE_ISSUE && "请选择代理商".equals(hv_agentName.getRightText())){
                    Toast.makeText(mContext, "请选择代理商", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(listener != null){
                    listener.onComplete(mode);
                }
                dismiss();
                break;
        }
    }

    public interface OnCompleteListener{
        void onComplete(int mode);
    }
}
